//
//  AccountLoginView.m
//  HomeDelivery
//
//  Created by apple on 16/7/21.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "AccountLoginView.h"

@implementation AccountLoginView
-(id)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame]) {
        [self setUpTxView];
    }
    return self;
}
#pragma mark--创建输入框所在的view
-(void)setUpTxView
{
   _whiteView=[[UIView alloc]initWithFrame:CGRectMake(0, 20, self.frame.size.width, 101)];
    _whiteView.backgroundColor=[UIColor whiteColor];
    [self addSubview:_whiteView];
    UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(0, 50,WIDTH, 1)];
    lineView.backgroundColor=LIGHTGRAY;
    [_whiteView addSubview:lineView];
    
    for (int i=0; i<2; i++) {
        UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(10, i*(30+21)+10, 30, 30)];
        image.image=[UIImage imageNamed:@[@"login_moible",@"password"][i]];
        [_whiteView addSubview:image];
        
        UITextField *tx=[[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(image.frame), i*(50+1), WIDTH-50, 50)];
        if (i==0) {
            tx.keyboardType=UIKeyboardTypePhonePad;
            
        }
        tx.delegate=self;
        tx.tag=(i+1)*100;
        tx.clearButtonMode=UITextFieldViewModeWhileEditing;
        tx.font=[UIFont systemFontOfSize:15];
        tx.textColor=[UIColor darkGrayColor];
        tx.placeholder=@[@"请输入手机号码/账户ID",@"请输入密码"][i];
        if (i==1) {
            tx.secureTextEntry=YES;
        }
        [_whiteView addSubview:tx];
    }
    //获取验证码按钮
    UIButton *codeBtn=[[UIButton alloc]initWithFrame:CGRectMake(WIDTH-10-100,CGRectGetMaxY(_whiteView.frame)+10, 100, 30)];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"忘记密码？"];
    NSRange strRange = {0,[str length]};
    [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:strRange];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:strRange];
    [codeBtn setAttributedTitle:str forState:UIControlStateNormal];
    [codeBtn addTarget:self action:@selector(forgetPassWord) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:codeBtn];
    //登录按钮
    UIButton *loginBtn=[[UIButton alloc]initWithFrame:CGRectMake(10,CGRectGetMaxY(_whiteView.frame)+50, WIDTH-20, 50)];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginBtn.backgroundColor=ORANGE;
    loginBtn.layer.cornerRadius=5;
    loginBtn.clipsToBounds=YES;
    [loginBtn addTarget:self action:@selector(goLogin) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:loginBtn];
    
}
//忘记密码
-(void)forgetPassWord
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"pushToForgetPsw" object:nil];
}
//登录
-(void)goLogin
{
    UITextField *account=[_whiteView viewWithTag:100];
    UITextField *passWord=[_whiteView viewWithTag:200];
    if (account.text.length==0) {
        [MBProgressHUD showError:@"请输入账号"];
    }else if (passWord.text.length==0)
    {
        [MBProgressHUD showError:@"请输入密码"];
  
    }else
    {
        //http://192.168.1.107:3722/User/AccountLogin
        //m_LoginPara(AdminAccount,AdminPwd,Timestamp,Sign)
        NSString *timeStamp=[GeneralTool gettimestamp];
        NSString *password=[passWord.text MD5];
        NSDictionary *dic=@{@"AdminAccount":account.text,
                            @"AdminPwd":password,
                            @"ClientType":CLIENTTYPE,
                            @"Timestamp":timeStamp};
        NSString *sign=[GeneralTool GetSin:dic];
        NSDictionary *dataDic=@{@"AdminAccount":account.text,
                            @"AdminPwd":password,
                            @"ClientType":CLIENTTYPE,
                            @"Timestamp":timeStamp,
                            @"Sign":sign};
        NSDictionary *requestDic=@{@"m_LoginPara":dataDic};
        [RequestTool requestDataWithType:@"post" andUrlString:@"User/AccountLogin" andParameters:requestDic andFinishedWith:^(id response, NSError *error) {
            if (error)
            {
                [MBProgressHUD showError:@"请求失败"];
            }else
            {
                if ([response[@"IsSuccess"]integerValue]==1)
                {
                    NSDictionary *dic=response[@"ReturnObject"];
                    NSUserDefaults *data=[NSUserDefaults standardUserDefaults];
                    [data setObject:dic[@"AdminAccount"] forKey:@"AdminAccount"];
                    [data setObject:dic[@"MerchantAccId"] forKey:@"MerchantAccId"];
                    [data synchronize];
                    
                    TabBarViewController *tabBar=[[TabBarViewController alloc]init];
                    UIWindow *window=[UIApplication sharedApplication].keyWindow;
                    window.rootViewController=tabBar;
                    /*
                     ReturnObject =     {
                     AdminAccount = 10001001;
                     CityCode = 310100;
                     IsFirstLogin = 0;
                     MerchantAccId = 100010;
                     Mobile = 13127513977;
                     };
                     
                     */
                
                }else
                {
                    [MBProgressHUD showError:response[@"ReturnString"]];

                }
            }
            
        }];
    }

}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self endEditing:YES];
    return YES;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self endEditing:YES];
}

@end
